package fr.inserm.u674.chcdbweb.model.forms;

import javax.servlet.http.HttpServletRequest;

import fr.inserm.u674.chcdbweb.model.DAO.DAOException;
import fr.inserm.u674.chcdbweb.model.DAO.TissueDAO;
import fr.inserm.u674.chcdbweb.model.DAO.TissueExtractionDAO;
import fr.inserm.u674.chcdbweb.model.DTO.Tissue;

public class DisplayTissueForm extends Form {

	// Constants
	// ---------------------------------------------------------------------------------
	private static final String FIELD_CHCID = "chcID";
	private static final String FIELD_TISSUETYPEID = "tissueTypeID";

	private static final String FIELD_RESULT = "result";

	// Variables
	// ---------------------------------------------------------------------------------
	private TissueDAO tissueDAO;
	private TissueExtractionDAO extractionDAO;

	// Constructor
	// -------------------------------------------------------------------------------
	public DisplayTissueForm(TissueDAO tissueDAO) {
		this.tissueDAO = tissueDAO;
	}

	public DisplayTissueForm(TissueDAO tissueDAO, TissueExtractionDAO extractionDAO) {
		this.extractionDAO = extractionDAO;
		this.tissueDAO = tissueDAO;
	}

	// Form actions

	public Tissue displayTissue(HttpServletRequest request) {
		Tissue tissue = new Tissue();
		try {
			processCHCID(request, tissue);
			processTissueTypeID(request, tissue);
			if (isSuccess()) {
				tissue = tissueDAO.find(tissue.getChcID(), tissue.getTissueTypeID());
				if (tissue != null) {
					tissueDAO.fetchTissueQuantityAfterLastManipulation(tissue);
					tissueDAO.listAssociatedTissues(tissue);
					tissueDAO.fetchTissueEAVData(tissue);
					extractionDAO.listTissueExtractions(tissue);
					setMessage(FIELD_RESULT, "Tissue " + tissue + " successfully retrieved.");
				}
				else {
					tissue =
						new Tissue(new Integer(FormUtil.getFieldValue(request, FIELD_CHCID)),
							FormUtil.getFieldValue(request, FIELD_TISSUETYPEID));
					setError(FIELD_RESULT, "Tissue " + tissue + " does not exist in the database.");
				}
			}
		}
		catch (DAOException e) {
			setError(FIELD_RESULT, "Tissue retrieval failed due to a database error."
				+ " Please try again later. Detailed message : " + e.getMessage());
			e.printStackTrace();
		}

		return tissue;
	}

	// Field processors
	// --------------------------------------------------------------------------

	/**
	 * Retrieves the chcID contained in the HTTP request, checks if it is valid, and sets it to the
	 * chcID field of the Tissue object
	 * 
	 * @param request
	 * @param tissue
	 * @throws DAOException
	 */
	protected void processCHCID(HttpServletRequest request, Tissue tissue) throws DAOException {
		String chcID = FormUtil.getFieldValue(request, FIELD_CHCID);
		Integer chcIDtemp = null;
			try {
				chcIDtemp = validateCHCID(chcID);
			}
			catch (ValidatorException e) {
				setError(FIELD_CHCID, e.getMessage());
			}
			tissue.setChcID(chcIDtemp);
	}

	/**
	 * Retrieves the tissueTypeID contained in the HTTP request, checks if it is valid, and sets it
	 * to the tissueTypeID field of the Tissue object
	 * 
	 * @param request
	 * @param tissue
	 * @throws DAOException
	 */
	protected void processTissueTypeID(HttpServletRequest request, Tissue tissue) throws DAOException {
		String tissueTypeID = FormUtil.getFieldValue(request, FIELD_TISSUETYPEID);
			try {
				validateTissueTypeID(tissueTypeID);
			}
			catch (ValidatorException e) {
				setError(FIELD_TISSUETYPEID, e.getMessage());
			}
			tissue.setTissueTypeID(tissueTypeID);
	}

	// Field validators ---------------------------------------------------------------------------

	protected Integer validateCHCID(String putativeCHCID) throws ValidatorException, DAOException {
		Integer chcID = null;
		if(FormUtil.isEmpty(putativeCHCID))
			throw new ValidatorException("The CHC ID cannot be null.");
		if (putativeCHCID != null) {
			try {
				chcID = new Integer(putativeCHCID);
				if (chcID < 0) {
					throw new ValidatorException("a CHC ID cannot be negative.");
				}
				else if (!tissueDAO.existsCHCID(chcID)) {
					throw new ValidatorException("This CHC ID does not exist in the database.");
				}
			}
			catch (NumberFormatException e) {
				throw new ValidatorException("Please enter a valid Number.");
			}
		}
		return chcID;
	}

	protected void validateTissueTypeID(String tissueTypeID) throws ValidatorException, DAOException {
		if(FormUtil.isEmpty(tissueTypeID))
			throw new ValidatorException("The Tissue Type ID cannot be null.");
		if (tissueTypeID != null) {
			if (tissueTypeID.length() > 5) {
				throw new ValidatorException("A Tissue Type ID cannot be longer than 5 characters.");
			}
			else if (!tissueDAO.existsTissueTypeID(tissueTypeID)) {
				throw new ValidatorException("This Tissue Type ID does not exist in the database.");
			}
		}
	}
}
